﻿using System.Linq;
using System.Web.Routing;
using System.Collections.Generic;
namespace System.Web.Mvc
{
    public class NetDefaultControllerFactory : DefaultControllerFactory
    {
        protected override Type GetControllerType(RequestContext requestContext, string controllerName)
        {
            object obj2;
            Type type;
            if (string.IsNullOrEmpty(controllerName))
                throw new ArgumentNullException("controllerName");
            if ((requestContext != null) && requestContext.RouteData.DataTokens.TryGetValue("Namespaces", out obj2))
            {
                var source = (obj2 as IEnumerable<string>);
                if ((source != null) && source.Any<string>())
                {
                    var namespaces = new HashSet<string>(source, StringComparer.OrdinalIgnoreCase);
                    type = GetControllerTypeWithinNamespaces(requestContext.RouteData.Route, controllerName, namespaces);
                    if (type == null)
                    {
                        bool flag = false;
                        if (!flag.Equals(requestContext.RouteData.DataTokens["UseNamespaceFallback"]))
                            goto MyLabel;
                    }
                    return type;
                }
            }
        MyLabel:
            if (ControllerBuilder.Current.DefaultNamespaces.Count > 0)
            {
                var namespaces = new HashSet<string>(ControllerBuilder.Current.DefaultNamespaces, StringComparer.OrdinalIgnoreCase);
                type = GetControllerTypeWithinNamespaces(requestContext.RouteData.Route, controllerName, namespaces);
                if (type != null)
                    return type;
            }
            return GetControllerTypeWithinNamespaces(requestContext.RouteData.Route, controllerName, null);
        }

        private Type GetControllerTypeWithinNamespaces(RouteBase route, string controllerName, HashSet<string> namespaces)
        {
            return null;
            //ControllerTypeCache.EnsureInitialized(BuildManager);
            //var controllerTypes = ControllerTypeCache.GetControllerTypes(controllerName, namespaces);
            //switch (controllerTypes.Count)
            //{
            //    case 0:
            //        return null;
            //    case 1:
            //        return controllerTypes.First<Type>();
            //}
            //throw CreateAmbiguousControllerException(route, controllerName, controllerTypes);
        }
    }
}